import re
class datacontent:
    def __init__(self,input_str):
        self.filename = "../data/douban.txt"
        self.dataArr, self.text = self.get_data()
        self.dataArr_vectors,self.char_to_n = self.get_data_vectors()
        self.input_vectors = self.get_input_vectors(input_str)


    def get_data(self):
        text = (open(self.filename).read())
        ## 将大写转换为小写
        # text = text.lower()
        # re模块多种情况切割
        strArr = re.split(',| |\n', text)
        ## list去重并排序
        strArr = sorted(list(set(strArr)))
        return strArr,text
    def get_data_vectors(self):
        # 字符映射成数字
        char_to_n = {char: n for n, char in enumerate(self.dataArr)}
        n_to_char = {n: char for n, char in enumerate(self.dataArr)}
        ## 数据向量化
        X = {}
        i = 0
        for str in self.text.split("\n"):
            i += 1
            if i < len(self.text.split("\n")):
                dataArr = re.split(',| |\n', str)
                X[dataArr[0]] = [char_to_n[strInde] for strInde in dataArr]
        return X,char_to_n

    def get_input_vectors(self,input_str):
        X_input = []
        # strdata = input_str.lower()
        dataArr = re.split(',| |\n', input_str)
        X_input.append([self.char_to_n[strInde] for strInde in dataArr])
        return X_input
# datacontent()

